var user;
apiready = function() {
	api.addEventListener({
		name: 'viewappear'
	}, function(ret, err) {
		init();
	});
};

function init() {
	$api.getUserInf(function(userInf) {
		user = userInf;
		api.showProgress();
		$api.do_post('/api/property/NoPayment', {
			body: JSON.stringify({
				zycode: userInf.zycode
			})
		}, function(re) {
			console.error("我刷新了");
			var rTotal = 0;
			var html = '';
			var yearmonth = '';
			if (re.length > 0) {
				re.forEach(function(item) {
					rTotal += item.qsAccount;
					if (yearmonth != item.yearmonth) {
						yearmonth = item.yearmonth;
						html += '<div class="payment_data clearfix"><input type="button" value="缴费" class="btn_normal" onclick="paymentData(\'' + item.yearmonth + '\')"/><span>' + item.yearmonth + '</span></div>';
					}
					html += '<ul class="pay_list"><li><span>¥' + item.qsAccount + '</span><b>' + item.project + '</b></li></ul>';

				});
				$('#paymentContent').html(html);
				$('#paymentTotal').text('总计：￥' + rTotal.toFixed(2));
				$('#paymentTip').remove();
			} else {
				$('#payAllBtn').remove();
			}
		});

	});
}

$('#payAllBtn').on('click', function() {
	api.showProgress();
	$api.do_post('/api/property/payment/order/create', {
		body: $api.jsonToStr({
			zycode: user.zycode,
			payMonth: 'all',
			account: user.account
		})
	}, function(result) {
		if (result.code == "0") {
			$api.mOpenWin('orderPay', '../orderpay.html', {
				payAmount: result.payAmount,
				orderNo: result.orderNo
			});
		} else {
			console.error("创建订单失败:" + result.msg)
		}
	});
});

function paymentData(month) {
	api.showProgress();
	$api.do_post('/api/property/payment/order/create', {
		body: JSON.stringify({
			zycode: user.zycode,
			payMonth: month,
			account: user.account
		})
	}, function(result) {
		$api.log(result)
		if (result.code == "0") {
			$api.mOpenWin('orderPay', '../orderpay.html', {
				payAmount: result.payAmount,
				orderNo: result.orderNo
			});
		} else {
			console.error("创建订单失败:" + result.msg)
		}
	});


}

// <%
//            var yearmonth="";
//            for(var i=0;i<locals.datas.length;i++){ 
//                rTotal+=Number(locals.datas[i].qsAccount);
//            %>
//                <%if(yearmonth!=locals.datas[i].yearmonth+''){
//                    yearmonth=locals.datas[i].yearmonth;  
//                %>
//                    <div class="payment_data clearfix">
//                        <input type="button" value="缴费" class="btn_normal" onclick="paymentData('<%=locals.datas[i].yearmonth%>')"/>
//                        <span><%=locals.datas[i].yearmonth%></span>
//                    </div>
//                <%}%>

//                <ul class="pay_list">
//                    <li><span>¥<%=locals.datas[i].qsAccount%></span><b><%=locals.datas[i].project%></b></li>
//                </ul>
//            <%}%>